package common

import (
	"Gin-Server-01/com/server/core/config"
	"Gin-Server-01/com/server/core/logs"
	"Gin-Server-01/com/server/core/tools/jsonTool"

	//"Gin-Server-01/com/server/core/tools/jsonTool"
	"Gin-Server-01/com/server/core/tools/yaml"

	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

/**
 * @Author XueZhimin
 * @Description
 * @Date 2024年3月10日20:55:05
 * @Version 1.0
 */

// TokenKey token的key
var TokenKey = "token"

// TokenAlive 存活时间
var TokenAlive = time.Minute * time.Duration(30)

// Configs 全局配置
var Configs *config.Config

var Banner = `
                                                                                                                                                     
                                                                    ''''''                                                                           
LLLLLLLLLLL                                          tttt           '::::'                              GGGGGGGGGGGGG                            !!! 
L:::::::::L                                       ttt:::t           '::::'                           GGG::::::::::::G                           !!:!!
L:::::::::L                                       t:::::t           ':::''                         GG:::::::::::::::G                           !:::!
LL:::::::LL                                       t:::::t          ':::'                          G:::::GGGGGGGG::::G                           !:::!
  L:::::L                   eeeeeeeeeeee    ttttttt:::::ttttttt    ''''       ssssssssss         G:::::G       GGGGGG        ooooooooooo        !:::!
  L:::::L                 ee::::::::::::ee  t:::::::::::::::::t             ss::::::::::s       G:::::G                    oo:::::::::::oo      !:::!
  L:::::L                e::::::eeeee:::::eet:::::::::::::::::t           ss:::::::::::::s      G:::::G                   o:::::::::::::::o     !:::!
  L:::::L               e::::::e     e:::::etttttt:::::::tttttt           s::::::ssss:::::s     G:::::G    GGGGGGGGGG     o:::::ooooo:::::o     !:::!
  L:::::L               e:::::::eeeee::::::e      t:::::t                  s:::::s  ssssss      G:::::G    G::::::::G     o::::o     o::::o     !:::!
  L:::::L               e:::::::::::::::::e       t:::::t                    s::::::s           G:::::G    GGGGG::::G     o::::o     o::::o     !:::!
  L:::::L               e::::::eeeeeeeeeee        t:::::t                       s::::::s        G:::::G        G::::G     o::::o     o::::o     !!:!!
  L:::::L         LLLLLLe:::::::e                 t:::::t    tttttt       ssssss   s:::::s       G:::::G       G::::G     o::::o     o::::o      !!! 
LL:::::::LLLLLLLLL:::::Le::::::::e                t::::::tttt:::::t       s:::::ssss::::::s       G:::::GGGGGGGG::::G     o:::::ooooo:::::o          
L::::::::::::::::::::::L e::::::::eeeeeeee        tt::::::::::::::t       s::::::::::::::s         GG:::::::::::::::G     o:::::::::::::::o      !!! 
L::::::::::::::::::::::L  ee:::::::::::::e          tt:::::::::::tt        s:::::::::::ss            GGG::::::GGG:::G      oo:::::::::::oo      !!:!!
LLLLLLLLLLLLLLLLLLLLLLLL    eeeeeeeeeeeeee            ttttttttttt           sssssssssss                 GGGGGG   GGGG        ooooooooooo         !!! 
                                                                                                                                                     
`

// GinControllerRegister gin框架控制器注册器
var GinControllerRegister = map[uint]func() *gin.HandlerFunc{}

var TestUserName = "root"
var TestUserPassword = "1234"

func init() {
	logs.Blue("正在读取配置文件....")
	Configs = yaml.ParseYAMLDefaultFileNoErr()
	log.Println("==============[配置文件]==================")
	log.Println("================================")
	log.Println("================================")
	logs.Green(jsonTool.JsonFormatString(Configs) + "\n")
	log.Println("================================")
	log.Println("================================")
	log.Println(Banner)
}

// GenToken
//
//	@Description: 生成token
//	@Param c
//	@Param token
//	@Param maxAge

func GenToken(c *gin.Context, token string, maxAge time.Duration) {

	// 删除现有的cookie
	http.SetCookie(c.Writer, &http.Cookie{Name: TokenKey, MaxAge: -1})

	c.Request.Cookie(TokenKey)
	//设置请求头token
	c.Header(TokenKey, token)
	//设置cookie
	c.SetCookie(TokenKey, token, int(maxAge.Seconds()), "/", "", false, true)
}
